Option Strict Off
Option Explicit On
Friend Class frmRotoscope
	Inherits System.Windows.Forms.Form
	Dim gRegFilters As Object
	Dim gCapStill As CAPSTILLLib.VBGrabber
	Private Sub frmRotoscope_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load ' Load Form
		Me.Width = VB6.TwipsToPixelsX(660 * VB6.TwipsPerPixelX)
		Me.Height = VB6.TwipsToPixelsY(547 * VB6.TwipsPerPixelY)
		Me.Left = VB6.TwipsToPixelsX((VB6.PixelsToTwipsX(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width) / 2) - ((660 / 2) * VB6.TwipsPerPixelX)) 'Set the window to be in the middle of the screen
		Me.Top = VB6.TwipsToPixelsY((VB6.PixelsToTwipsY(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height) / 2) - (((547 / 2) + 28) * VB6.TwipsPerPixelY)) 'Set the window raised up slightly. So it is better positioned when running at 1024 x 768
		RotoScopeSyncYesNo = False
		'UPGRADE_WARNING: Add a delegate for AddressOf NewWindowProc1 Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="E9E157F7-EF0C-4016-87B7-7D7FBBC6EE08"'
		OldWindowProc = SetWindowLong(Handle.ToInt32, GWL_WNDPROC, AddressOf NewWindowProc1) 'Fix aspect ratio
		DisableCloseWindowButton(Me)
	End Sub
	Private Sub cmdOpen_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOpen.Click
		
		
		Call RemoveDShowRotoscope() 'Clean up
		
		
		RotoScopeMediaControl = New QuartzTypeLib.FilgraphManager
		gRegFilters = RotoScopeMediaControl.RegFilterCollection 'gather all the filter names from the system
		
		dlgFileOpen.ShowDialog() 'open a dialog so the user can select a file
		
		On Error GoTo NoCodec
		RotoScopeMediaControl.RenderFile((dlgFileOpen.FileName)) 'generate the complete graph (linked filters) automatically to suit the file selected by the user above
		
		'   On Error Resume Next
		
		dx_VideoWindow = RotoScopeMediaControl 'Attach the media control to the video window
		dx_VideoWindow.WindowStyle = 0 'Set the window style to none.
		dx_VideoWindow.Top = 25 'Ensure the video window is in the right place over the picturebox
		dx_VideoWindow.Left = 0
		dx_VideoWindow.Width = 640
		dx_VideoWindow.Height = 480
		dx_VideoWindow.Owner = Me.Handle.ToInt32 'Play the video only on the destination
		
		RotoScopeMediaPosition = RotoScopeMediaControl 'enable seeking etc.
		
		RotoScopeLoadedYesNO = True 'Tell the program that film is loaded,the capture can now advance it without problems
		RotoScopeMediaControl.Run() 'Play and pause video so first frame shows in window
		RotoScopeMediaControl.Pause()
		Exit Sub
NoCodec: 
		If dlgFileOpen.FileName <> "" Then 'just end if no file has been chosen, but if there are any other errors tell user
			Call MsgBox("I cannot find the codec to run this file or the file contains no video", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
		End If
	End Sub
	
	Private Sub cmdQuit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdQuit.Click
		If RotoScopeSyncYesNo = True Then
			If MsgBox("The Video Syncronisation will be lost" & Chr(13) & Chr(10) & "OK To continue?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Helium Frog Animator") = MsgBoxResult.No Then
				Exit Sub
			End If
		End If
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'If ObjPtr(RotoScopeMediaControl) > 0 Then RotoScopeMediaControl.Pause() 'If the RotoScopeMediaControl exists, then pause .avi if running
        If Not RotoScopeMediaControl Is Nothing Then RotoScopeMediaControl.Pause() 'If the RotoScopeMediaControl exists, then pause .avi if running
		RotoScopeActiveYesNo = False 'Tell the main loop that rotoscope window is not showing
		RotoScopeSyncYesNo = False 'DeSync the video so it doesn't capture every frame
		Me.Visible = False 'Hide the form
	End Sub
	
	
	Private Sub cmdSync_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSync.Click
		If RotoScopeSyncYesNo = True Then
			RotoScopeSyncYesNo = False
		Else
			RotoScopeSyncYesNo = True
		End If
	End Sub
	
	
	Private Sub frmRotoscope_FormClosing(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		Dim Cancel As Boolean = eventArgs.Cancel
		Dim UnloadMode As System.Windows.Forms.CloseReason = eventArgs.CloseReason
		If UnloadMode = System.Windows.Forms.CloseReason.UserClosing Then
			Cancel = True 'Prevent the window from being closed by the 'X' Button
		End If
		eventArgs.Cancel = Cancel
	End Sub
	
	
	Private Sub cmdBack1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBack1.Click 'Move back 1 frame (capture frames per second, not .avi frames
		If RotoScopeLoadedYesNO = False Then Exit Sub 'There is no film loaded, so exit subroutine
		If RotoScopeMediaPosition.CurrentPosition - (1 / FPS) < 0 Then 'Check if this will take it back beyond the start of the .avi
			Exit Sub
		Else
			RotoScopeMediaPosition.CurrentPosition = RotoScopeMediaPosition.CurrentPosition - (1 / FPS)
			RotoScopeMediaControl.Pause()
		End If
	End Sub
	
	Private Sub cmdForward1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdForward1.Click
		If RotoScopeLoadedYesNO = False Then Exit Sub 'There is no film loaded, so exit subroutine
		If RotoScopeMediaPosition.CurrentPosition + (1 / FPS) > RotoScopeMediaPosition.Duration Then 'Check if this will take it past end of the .avi
			Exit Sub
		Else
			RotoScopeMediaPosition.CurrentPosition = RotoScopeMediaPosition.CurrentPosition + (1 / FPS)
		End If
	End Sub
	
	Private Sub cmdPause_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdPause.Click
		If RotoScopeLoadedYesNO = False Then Exit Sub 'There is no film loaded, so exit subroutine
		RotoScopeMediaControl.Pause()
	End Sub
	
	Private Sub cmdPlay_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdPlay.Click
		If RotoScopeLoadedYesNO = False Then Exit Sub 'There is no film loaded, so exit subroutine
		RotoScopeMediaControl.Run()
	End Sub
	
	Private Sub cmdToEnd_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdToEnd.Click
		If RotoScopeLoadedYesNO = False Then Exit Sub 'There is no film loaded, so exit subroutine
		RotoScopeMediaPosition.CurrentPosition = RotoScopeMediaPosition.Duration
	End Sub
	
	Private Sub cmdToStart_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdToStart.Click
		If RotoScopeLoadedYesNO = False Then Exit Sub 'There is no film loaded, so exit subroutine
		RotoScopeMediaPosition.CurrentPosition = 0
		RotoScopeMediaControl.Pause()
	End Sub
	
	Public Sub RemoveDShowRotoscope() 'clean up Rotoscope media player and variables
		
		On Error GoTo RemoveDShowError
		
		'If a MediaControl instance exists, then stop it from playing
		'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'If ObjPtr(RotoScopeMediaControl) > 0 Then
        If Not RotoScopeMediaControl Is Nothing Then
            RotoScopeMediaControl.Stop()
        End If
        'If a VideoWindow instance exists, then remove the link to the render target
        'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'If ObjPtr(RotoScopeVideoWindow) > 0 Then
        If Not RotoScopeVideoWindow Is Nothing Then
            RotoScopeVideoWindow.Owner = 0
        End If
        'Destroy all objects
        'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'UPGRADE_NOTE: Object RotoScopeBasicAudio may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        'If ObjPtr(RotoScopeBasicAudio) > 0 Then RotoScopeBasicAudio = Nothing
        If Not RotoScopeBasicAudio Is Nothing Then RotoScopeBasicAudio = Nothing
        'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'UPGRADE_NOTE: Object RotoScopeBasicVideo may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        'If ObjPtr(RotoScopeBasicVideo) > 0 Then RotoScopeBasicVideo = Nothing
        If Not RotoScopeBasicVideo Is Nothing Then RotoScopeBasicVideo = Nothing
        'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'UPGRADE_NOTE: Object RotoScopeMediaControl may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        'If ObjPtr(RotoScopeMediaControl) > 0 Then RotoScopeMediaControl = Nothing
        If Not RotoScopeMediaControl Is Nothing Then RotoScopeMediaControl = Nothing
        'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'UPGRADE_NOTE: Object RotoScopeVideoWindow may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        'If ObjPtr(RotoScopeVideoWindow) > 0 Then RotoScopeVideoWindow = Nothing
        If Not RotoScopeVideoWindow Is Nothing Then RotoScopeVideoWindow = Nothing
        'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'UPGRADE_NOTE: Object RotoScopeMediaPosition may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        'If ObjPtr(RotoScopeMediaPosition) > 0 Then RotoScopeMediaPosition = Nothing
        If Not RotoScopeMediaPosition Is Nothing Then RotoScopeMediaPosition = Nothing
        Exit Sub
RemoveDShowError:
        Err.Clear()
        Exit Sub
    End Sub
End Class